﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace JXT.PrimaryKey.Batman.Core.Configuration
{
    /// <summary>
    /// OAuth配置集合
    /// </summary>
    public class OauthSettingConfigurationElementCollection : ConfigurationElementCollection, IEnumerable<OauthConfigurationElement>
    {
        /// <summary>
        /// 默认AppKey
        /// </summary>
        [ConfigurationProperty("defaultAppKey", IsRequired = true)]
        public string DefaultAppKey
        {
            get { return (string)base["defaultAppKey"]; }
            set { base["defaultAppKey"] = value; }
        }

        /// <summary>
        /// 默认AppSecretKey
        /// </summary>
        [ConfigurationProperty("defaultAppSecret", IsRequired = true)]
        public string DefaultAppSecret
        {
            get { return (string)base["defaultAppSecret"]; }
            set { base["defaultAppSecret"] = value; }
        }

        public new OauthConfigurationElement this[string name]
        {
            get
            {
                return BaseGet(name) as OauthConfigurationElement;
            }
            set
            {
                if (BaseGet(name) != null)
                {
                    base.BaseRemove(name);
                }
                BaseAdd(value);
            }
        }

        public new IEnumerator<OauthConfigurationElement> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return (OauthConfigurationElement)BaseGet(i);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new OauthConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((OauthConfigurationElement)element).Name;
        }
    }
}
